package com.scalar.chiptrack.operations.inventoryps.actioncommands;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.inventoryps.forms.InventoryPackingSlipForm;
import com.scalar.chiptrack.operations.inventoryps.dao.InventoryPSDao;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.commons.Location;
import com.scalar.chiptrack.commons.ClientInfo;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;

public class InventoryPackingSlipViewCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/inventorypsSearch.do" ;
    
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{

        InventoryPackingSlipForm inventoryPSForm = (InventoryPackingSlipForm) form;
    	String forwardMapping = "confirmps";
        HashMap<String, String> errors = new HashMap<String, String>();
        HashMap<String, String> info = new HashMap<String, String>();
        String action = request.getParameter("action");
        String inventorypackingSlipFile = null;
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        inventoryPSForm.setPackingSlipId( Integer.parseInt( request.getParameter( "packingSlipId") ) );
        
        InventoryPSDao.loadInventoryPackingSlip( inventoryPSForm );

        inventoryPSForm.setSelectedLotsList( inventoryPSForm.getAvailableLotsList() );
        request.setAttribute( "action", action );
        
        if ("generatePDF".equalsIgnoreCase( action ))
        {
            inventorypackingSlipFile = InventoryPSDao.getInventoryPSAsPDFFile( request.getHeader ( "HOST" ), inventoryPSForm ) ;
            request.setAttribute( "PDFFileName", new File(inventorypackingSlipFile).getName() );

            return mapping.findForward( "generetedPDF" );
        }
        if( showWaferIds( inventoryPSForm.getSelectedLotsList() ) )
        {
            request.setAttribute( "showWaferIds", "TRUE" );
        }
        if ("printView".equalsIgnoreCase( action ) )
        {
            forwardMapping = "printView";
        }
		return mapping.findForward( forwardMapping );
	}
    private boolean showWaferIds(ArrayList<LotDetails> availableLotList)
	{
		boolean showWaferIds = false;
		if( availableLotList == null || availableLotList.size() == 0 )
		{
			showWaferIds = false;
		}
		else
		{
			for( LotDetails	lotDetail:availableLotList )
			{
				if( lotDetail.getWaferIds() != null && lotDetail.getWaferIds().length() > 0 )
				{
					showWaferIds = true;
					break;
				}
			}
		}
		return showWaferIds;
	}
}
